package com.binyouwei.designpatterns.creation_type.prototype;

import java.util.Hashtable;

/**
 * @author 宾有为
 * @date 2024/6/24 19:58
 * @link https://blog.csdn.net/baidu_41616022?type=blog
 * @describe：获取实体类，并把它们存储在一个 Hashtable 中
 **/
public class ShapeCache {
    private static Hashtable<String, PrototypeShape> shapeMap = new Hashtable<String, PrototypeShape>();

    public static PrototypeShape getShapeMap(String key) {
        PrototypeShape cacheShape = shapeMap.get(key);
        return (PrototypeShape) cacheShape.clone();
    }

    /**
     * 对每种形状都运行数据库查询，并创建该形状
     * 如下，我们要添加三种形状
     */
    public static void loadCache() {
        PrototypeCircle circle = new PrototypeCircle();
        circle.setId("1");
        shapeMap.put(circle.getId(), circle);
        PrototypeRectangle rectangle = new PrototypeRectangle();
        rectangle.setId("2");
        shapeMap.put(rectangle.getId(), rectangle);
    }
}
